package com.rover.system.controller;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.entity.FormTableInfo;
import com.rover.system.service.FormTableInfoService;
import com.rover.system.service.FormTableInfoService;
import com.utils.HqlGenerateUtil;
import com.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公开访问
 */
@Controller
@RequestMapping("/formTableInfoController")
public class FormTableInfoController {
	
    @Autowired
    private FormTableInfoService service;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request,Page page, HttpServletResponse response) throws Exception {
        HqlGenerateUtil.installHql(request,FormTableInfo.class,page);
          Map < String, Object > map = new HashMap<String, Object>();

        DataGrid dataGrid=  service.query(page);
        map = JsonUtil.JSONString(dataGrid.getResult(),
                dataGrid.getTotalCount());
		return map;
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(FormTableInfo formTableInfo, HttpServletRequest request)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
            if (service.isPhysicalNameRepeat(formTableInfo)) {
                message = "此表名已存在，请重新输入！";
                success=false;
            }
            else
                service.save(formTableInfo);
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            // logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestParam("tableId") List<Integer> tableId) {
        Map<String, Object> map = new HashMap<String, Object>();
        // String id = request.getParameter("id");
        try {
            service.delete(FormTableInfo.class,tableId);
            map.put("success", true);
            map.put("message", "");

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    /*
    * 生成通用页面
    * */
    @RequestMapping(value = "/generatePage", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> generatePage(@RequestParam("tableId") Integer tableId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            service.generatePage(tableId,request.getRealPath("/"));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }




}
